﻿using ZYCommon;
using System;
using System.Drawing;
using System.Text;
using System.Xml;

namespace TextEditor
{
    

    public class ImagePolyAction : ImageEditAction
    {
        public Color ForeColor = Color.Black;
        public int LineWidth = 1;
        private Point[] myPoints = null;

        public override bool DesignChangeBounds(Rectangle NewBounds)
        {
            Rectangle bounds = this.Bounds;
            int dx = NewBounds.X - bounds.X;
            int dy = NewBounds.Y - bounds.Y;
            for (int i = 0; i < this.myPoints.Length; i++)
            {
                this.myPoints[i].Offset(dx, dy);
            }
            base.myBounds = PointBuffer.GetBounds(this.myPoints);
            return true;
        }

        public override bool Execute(Graphics g, Rectangle ClipRect)
        {
            if (this.Points != null)
            {
                using (Pen pen = new Pen(this.ForeColor, (float) this.LineWidth))
                {
                    g.DrawLines(pen, this.Points);
                }
                return true;
            }
            return false;
        }

        public override bool FromXML(XmlElement myElement)
        {
            this.ForeColor = StringCommon.ColorFromHtml(myElement.GetAttribute("color"), Color.Black);
            this.LineWidth = Convert.ToInt32(myElement.GetAttribute("linewidth"));
            string[] strArray = myElement.GetAttribute("points").Split(",".ToCharArray());
            this.Points = null;
            if ((strArray != null) && (strArray.Length > 0))
            {
                this.Points = new Point[(int) Math.Floor((double) (strArray.Length / 2))];
                for (int i = 0; i < this.Points.Length; i++)
                {
                    this.Points[i].X = Convert.ToInt32(strArray[i * 2]);
                    this.Points[i].Y = Convert.ToInt32(strArray[(i * 2) + 1]);
                }
                base.myBounds = PointBuffer.GetBounds(this.Points);
            }
            return true;
        }

        public override bool ToXML(XmlElement myElement)
        {
            myElement.SetAttribute("color", StringCommon.ColorToHtml(this.ForeColor));
            myElement.SetAttribute("linewidth", this.LineWidth.ToString());
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < this.Points.Length; i++)
            {
                builder.Append(this.Points[i].X.ToString());
                builder.Append(",");
                builder.Append(this.Points[i].Y.ToString());
                if (i != (this.Points.Length - 1))
                {
                    builder.Append(",");
                }
            }
            myElement.SetAttribute("points", builder.ToString());
            return true;
        }

        public override string ActionName
        {
            get
            {
                return "poly";
            }
        }

        public override Rectangle Bounds
        {
            get
            {
                return base.myBounds;
            }
            set
            {
            }
        }

        public Point[] Points
        {
            get
            {
                return this.myPoints;
            }
            set
            {
                this.myPoints = value;
                base.myBounds = PointBuffer.GetBounds(value);
            }
        }
    }
}

